package cc.lj.internal.coupons.application.service;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.domain.martcap.service.ICapBatchDomainService;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.service.ITicketBatchDomainService;
import cc.lj.internal.coupons.interfaces.dto.CapBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.TicketBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.command.InsertCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.command.UpdateCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.convert.CapBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.convert.TicketBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.query.CapBatchQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 封顶额批次表 app service
 * @author liangxifeng
 * @date 2021-12-13
 */
@Service
@Slf4j
public class CapBatchAppService {
    //封顶额活动批次领域层
    @Resource(name = "capBatchDomainServiceImpl")
    private ICapBatchDomainService batchDomainService;

    //电子券批次领域层
    @Resource(name = "ticketBatchDomainServiceImpl")
    private ITicketBatchDomainService ticketBatchDomainService;

    /**
     * 保存活动批次表数据
     * @param cmd
     * @return 新增主键
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public Integer save(InsertCapBatchCommand cmd) {
        //DTO to DO
        CapBatchDO capBatchDO = CapBatchDTOConvertDO.INSTANCE.convert(cmd);
        //通过主键查询电子券批次信息
        TicketBatchDO ticketBatchDO = ticketBatchDomainService.getById(capBatchDO.getBatchId());
        capBatchDO.setBatchPrice(ticketBatchDO.getBatchPrice()); //赋值电子券面额

        //新增封顶额活动批次表数据
        Integer lastId = batchDomainService.save(capBatchDO);

        //生成活动编号
        String activityCode = batchDomainService.createActivityCode(lastId);

        //修改电子券批次表活动编号字段
        ticketBatchDomainService.bindActivityCode(capBatchDO.getBatchId(), activityCode);
        return lastId;
    }

    /**
     * 通过主键查询单条数据
     * @param id
     * @return 实体DTO
     */
    public CapBatchDTO getById(Integer id) {
        CapBatchDO capBatchDO =  batchDomainService.getById(id);
        //通过主键查询电子券批次DO
        TicketBatchDO ticketBatchDO = ticketBatchDomainService.getById(capBatchDO.getBatchId());
        //转换电子券批次DO为DTO
        TicketBatchDTO ticketBatchDTO = TicketBatchDTOConvertDO.INSTANCE.convert(ticketBatchDO);
        //转换封顶额活动批次DO to DTO
        CapBatchDTO capBatchDTO = CapBatchDTOConvertDO.INSTANCE.convert(capBatchDO);
        //封顶额活动DTO中电子券批次DTO属性赋值
        capBatchDTO.setTicketBatchDTO(ticketBatchDTO);
        return capBatchDTO;
    }

    /**
     * 通过主键修改
     * @param updateCmd
     * @return 影响行数
     */
    public Integer updateById(UpdateCapBatchCommand updateCmd) {
        //cmd to do
        CapBatchDO capBatchDO = CapBatchDTOConvertDO.INSTANCE.convert(updateCmd);
        log.info("app Service转换后的capBatchDO="+capBatchDO);
        //通过主键查询封顶额批次数据
        CapBatchDO queryDO = batchDomainService.getById(capBatchDO.getId());
        //通过主键查询电子券批次信息
        TicketBatchDO ticketBatchDO = ticketBatchDomainService.getById(queryDO.getBatchId());
        capBatchDO.setBatchPrice(ticketBatchDO.getBatchPrice()); //赋值电子券面额

        //修改封顶额批次数据
        Integer rows = batchDomainService.updateById(capBatchDO);
        return rows;
    }
    /**
     * 查询分页列表
     * @param pageParam
     * @return
     */
    public PageBean<CapBatchDTO> selectPage(PageParam<CapBatchQuery> pageParam) {
        //查询列表
        PageBean<CapBatchDO> pageBean = batchDomainService.selectPage(convert(pageParam));
        List<CapBatchDTO> capBatchListDTO = new ArrayList();
        //循环查询批券电子券批次数据，并赋值到CapBatchDTO中
        for (CapBatchDO record : pageBean.getRecords()) {
            //通过主键查询电子券批次DO
            TicketBatchDO ticketBatchDO = ticketBatchDomainService.getById(record.getBatchId());
            //转换电子券批次DO为DTO
            TicketBatchDTO ticketBatchDTO = TicketBatchDTOConvertDO.INSTANCE.convert(ticketBatchDO);
            //转换封顶额活动批次DO to DTO
            CapBatchDTO capBatchDTO = CapBatchDTOConvertDO.INSTANCE.convert(record);
            //封顶额活动DTO中电子券批次DTO属性赋值
            capBatchDTO.setTicketBatchDTO(ticketBatchDTO);
            //拼接最终返回的DTO列表
            capBatchListDTO.add(capBatchDTO);
        }
        //重新生成pageBean，做返回值
        PageBean pageBeanRes = PageBean.createPageBean(pageBean.getPageNum(),
                pageBean.getPageSize(), pageBean.getTotal(), capBatchListDTO);
        return pageBeanRes;
    }
    /**
     * DO to PO
     * @param pageParam
     * @return
     */
    public PageParam<CapBatchDO> convert(PageParam<CapBatchQuery> pageParam) {
        PageParam<CapBatchDO> pageParamR = new PageParam<>();
        pageParamR.setPageNum( pageParam.getPageNum() ) ;
        pageParamR.setPageSize( pageParam.getPageSize() );
        pageParamR.setOrderBy( pageParam.getOrderBy() );
        CapBatchDO capBatchDO = CapBatchDTOConvertDO.INSTANCE.convert(pageParam.getParam());
        pageParamR.setParam(capBatchDO);
        return pageParamR;
    }

    /**
     * 通过主键删除
     * @param id
     * @return 影响行数
     */
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public Integer deleteById(Integer id) {
        //通过主键查询封顶额批次数据
        CapBatchDO capBatchDO = batchDomainService.getById(id);
        //清空电子券批次表关联的活动编号字段
        Integer unBindBatchrows = ticketBatchDomainService.unBindActivityCode(capBatchDO.getBatchId());
        //通过主键删除封顶额批次数据
        Integer rows = batchDomainService.deleteById(id);
        return rows;
    }
}
